package com.hpin.mlxteacher.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.utils.DensityUtil;
import com.hpin.mlxteacher.utils.LogUtil;
import com.hpin.mlxteacher.widget.GridViewForScrollView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Aaron on 15/7/30.
 */
public class BabyTreeAdapter extends BaseAdapter {

    private Context ctx;
    private List<String> sizes;

    public BabyTreeAdapter(Context ctx, List<String> sizes) {
        this.ctx = ctx;
        this.sizes = sizes;
    }

    @Override
    public int getCount() {

//        LogUtil.e("TAG", "getCount" + sizes.size());
        return sizes.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;

        if (convertView == null) {

            convertView = View.inflate(ctx, R.layout.item_baby_tree, null);
            viewHolder = new ViewHolder();
            viewHolder.iv_head_pic = (ImageView) convertView.findViewById(R.id.iv_head_pic);
            viewHolder.iv_share = (ImageView) convertView.findViewById(R.id.iv_share);
            viewHolder.iv_comment = (ImageView) convertView.findViewById(R.id.iv_comment);

            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
            viewHolder.tv_loaction = (TextView) convertView.findViewById(R.id.tv_loaction);
            viewHolder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            viewHolder.tv_delete = (TextView) convertView.findViewById(R.id.tv_delete);

            viewHolder.gv_photos = (GridViewForScrollView) convertView.findViewById(R.id.gv_photos);
            viewHolder.ll_comments = (LinearLayout) convertView.findViewById(R.id.ll_comments);

            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        ArrayList<String> list = new ArrayList<String>();

        for (int i = 0; i < position + 1; i++) {
            list.add("http://www.huabian.com/uploadfile/2014/1118/20141118042246536.jpg");
        }
        ShowImageAdapter imageAdapter = new ShowImageAdapter(ctx, list);


        if (list.size() == 1) {//如果只有一张图片
            viewHolder.gv_photos.setNumColumns(1);

            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            viewHolder.gv_photos.setLayoutParams(lp);

        } else if (list.size() == 4) {//四张图片,显示正方形
            viewHolder.gv_photos.setNumColumns(2);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(viewHolder.gv_photos.getLayoutParams());
            lp.setMargins(0, 0, DensityUtil.dip2px(ctx, 85), 0);
            viewHolder.gv_photos.setLayoutParams(lp);

        } else {
            viewHolder.gv_photos.setNumColumns(3);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            viewHolder.gv_photos.setLayoutParams(lp);
        }

        viewHolder.gv_photos.setAdapter(imageAdapter);


        viewHolder.tv_content.setText("内容:收集整理Android开发所需的Android SDK、开发中用到的工具、Android开发教程、Android设计规范，免费的设计素材" + position);

        viewHolder.tv_delete.setOnClickListener(new View.OnClickListener() {  //删除
            @Override
            public void onClick(View v) {

                LogUtil.e("TAG", "删除的position====>>>>" + position);

                sizes.remove(position);

                for(String s : sizes) {
                    LogUtil.e("TAG" , "===>>>>" + s);
                }

                notifyDataSetChanged();
            }
        });

        viewHolder.iv_share.setOnClickListener(new View.OnClickListener() {  //分享
            @Override
            public void onClick(View v) {

            }
        });

        viewHolder.iv_comment.setOnClickListener(new View.OnClickListener() {  //评论
            @Override
            public void onClick(View v) {

            }
        });
        return convertView;
    }

    private class ViewHolder {
        private ImageView iv_head_pic;
        private TextView tv_name;
        private TextView tv_content;
        private GridViewForScrollView gv_photos;
        private TextView tv_loaction;
        private TextView tv_time;
        private TextView tv_delete;
        private ImageView iv_share;
        private ImageView iv_comment;
        private LinearLayout ll_comments;
    }


}
